# Copyright (c) 2007 Damian Yerrick
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.


# the following might differ per installation
CC65BASE := /e/nesdev/cc65
FCEU := /e/gams/nes/fceu080win/fceu.exe
EMU := /e/games/nes/nintendulator0900bin/Nintendulator.exe
#DOTEXE :=
DOTEXE := .exe

# PC side
CC := gcc
CFLAGS := -Wall -O -std=gnu99
LD := gcc
LDFLAGS := -Wall
CONVERT81 = tools/81$(DOTEXE)

# NES side
AS65 := $(CC65BASE)/bin/ca65
LD65 := $(CC65BASE)/bin/ld65
srcdir = src
objdir = obj/nes
NESROM := 81ex
NESOBJS := $(objdir)/81ex.o $(objdir)/dpcm.o
SAMPLES := $(objdir)/1bps.dmc

.PHONY: run clean zip

run: $(NESROM).nes
	$(EMU) $<

%.nes: $(srcdir)/%.hdr $(objdir)/%.prg $(srcdir)/%.chr
	cat $^ > $@

$(NESROM).map.txt $(objdir)/$(NESROM).prg: nes.ini $(NESOBJS)
	$(LD65) -C $^ -m $(NESROM).map.txt -o $(objdir)/$(NESROM).prg

$(objdir)/%.o: $(srcdir)/%.s $(srcdir)/nes.h
	$(AS65) $< -o $@

$(objdir)/dpcm.o: $(SAMPLES)

$(objdir)/%.dmc: $(srcdir)/%.wav $(CONVERT81)
	$(CONVERT81) $< $@

clean:
	-rm $(objdir)/*
	-rm tools/*.o
	-rm $(NESROM).map.txt

zip: 81ex.zip
81ex.zip: $(NESROM).nes zip.in
	zip -9 $@ -@ < zip.in
	

# PC side rules
$(CONVERT81): tools/81.o tools/readwav.o
	$(LD) $(LDFLAGS) $^ $(LDLIBS) -o $@
tools/81.o: tools/81.c tools/readwav.h
tools/readwav.o: tools/readwav.c tools/readwav.h

